#include "HD44780AnalyzerResults.h"
#include <AnalyzerHelpers.h>
#include "HD44780Analyzer.h"
#include "HD44780AnalyzerSettings.h"
#include "PluginConstants.h"
#include <iostream>
#include <fstream>
#include "ResultAnalyzer.h"
#include <iostream>
#include <sstream>
#include "ReportGenerator.h"



HD44780AnalyzerResults::HD44780AnalyzerResults( HD44780Analyzer* analyzer, HD44780AnalyzerSettings* settings )
:	AnalyzerResults(),
	mSettings( settings ),
	mAnalyzer( analyzer )
{
    CreateResultAnalyzers();
    CreateReportGenerators();
}

void HD44780AnalyzerResults::CreateResultAnalyzers()
{
    DeleteResultAnalyzers();
    mAnalyzerList.push_back(new ErrorRequestAnalyzer());
    mAnalyzerList.push_back(new WriteRequestAnalyzer());
    mAnalyzerList.push_back(new ReadBusyFlagRequestAnalyzer());
    mAnalyzerList.push_back(new ReadDataRequestAnalyzer());
    mAnalyzerList.push_back(new ControlRequestAnalyzer());
    mAnalyzerList.push_back(new ErrorRequestAnalyzer8Bits());
    mAnalyzerList.push_back(new WriteRequestAnalyzer8Bits());
    mAnalyzerList.push_back(new ReadBusyFlagRequestAnalyzer8Bits());
    mAnalyzerList.push_back(new ReadDataRequestAnalyzer8Bits());
    mAnalyzerList.push_back(new ControlRequestAnalyzer8Bits());
}

void HD44780AnalyzerResults::DeleteResultAnalyzers()
{
    int Size = mAnalyzerList.size();
    for ( int i =0 ; i < Size; ++i )
    {
        delete mAnalyzerList[i];
    }
    mAnalyzerList.clear();
}

void HD44780AnalyzerResults::CreateReportGenerators()
{
    DeleteReportGenerators();
    mReportGeneratorList.push_back(new CSVReportGenerator(this,mSettings));
    mReportGeneratorList.push_back(new SequenceDiagramReportGenerator(this,mSettings));
}

void HD44780AnalyzerResults::DeleteReportGenerators()
{
    int Size = mReportGeneratorList.size();
    for ( int i =0 ; i < Size; ++i )
    {
        delete mReportGeneratorList[i];
    }
    mReportGeneratorList.clear();
}

HD44780AnalyzerResults::~HD44780AnalyzerResults()
{
    DeleteResultAnalyzers();
    DeleteReportGenerators();
}

ResultAnalyzer* HD44780AnalyzerResults::GetAnalyzer( U8 Index )
{
    return mAnalyzerList[ Index ];
}

void HD44780AnalyzerResults::GenerateBubbleText( U64 frame_index, Channel& channel, DisplayBase display_base )
{
    ClearResultStrings();
    Frame TargetFrame = GetFrame( frame_index );
     AddResultString( GetAnalyzer( TargetFrame.mType )->Analyze( TargetFrame ) );
}

void HD44780AnalyzerResults::GenerateExportFile( const char* file, DisplayBase display_base, U32 export_type_user_id )
{
    mReportGeneratorList[export_type_user_id]->GenerateReport( file, display_base);
}

void HD44780AnalyzerResults::GenerateFrameTabularText( U64 frame_index, DisplayBase display_base )
{
	Frame frame = GetFrame( frame_index );
	ClearResultStrings();

	char number_str[128];
	AnalyzerHelpers::GetNumberString( frame.mData1, display_base, 8, number_str, 128 );
	AddResultString( number_str );
}

void HD44780AnalyzerResults::GeneratePacketTabularText( U64 packet_id, DisplayBase display_base )
{
	ClearResultStrings();
	AddResultString( "not supported" );
}

void HD44780AnalyzerResults::GenerateTransactionTabularText( U64 transaction_id, DisplayBase display_base )
{
	ClearResultStrings();
	AddResultString( "not supported" );
}

U32 HD44780AnalyzerResults::GetSampleRate() { return mAnalyzer->GetSampleRate(); }
U64 HD44780AnalyzerResults::GetTriggerSample() { return mAnalyzer->GetTriggerSample(); }

